<?php
require_once 'BaseMySQLDAO.php';

class EmailDAO extends BaseMySQLDAO{
	private $context;
	
	function __construct($context, $entityName="email") {
		parent::__construct($context, $entityName);
	}
	public function fillEmailTemplate(Notification $notification) {
		$this->getContext()->getDBTransaction()->startTransaction();
		if(!$notification->hasNoTemplate())
		{
			$inSQL = "	SELECT email.text, email.html, email.subject 
						FROM email 
						WHERE email.lang ='ENG' AND email.type ='".$notification->getType()."' LIMIT 1";
			//echo $inSQL;
			$result = $this->getMySQLConnection()->mysqlOneRow($inSQL);
			$notification->setTemplate($result['subject'], $result['text'], $result['html']);
		}
		$this->getContext()->getDBTransaction()->closeTransaction();
		return $notification;
	}
	
	public function create($obj){
		$this->getContext()->getDBTransaction()->startTransaction();
		$inSQL = "INSERT INTO email(lang,type,subject,text,html)
					VALUES	(
								'".$obj->getLang()."',
								'".$obj->getType()."',
								'".$obj->getSubject()."',
								'".$obj->getText()."',
								'".$obj->getHtml()."'
							)";
		echo $inSQL;
		$this->getMySQLConnection()->mysqlData($inSQL);
		$this->getContext()->getDBTransaction()->closeTransaction();
	}

	public function deleteByOid($oid){
		$this->getContext()->getDBTransaction()->startTransaction();
		$inSQL = "DELETE FROM email WHERE mail_id=".$oid;
		$this->getMySQLConnection()->mysqlData($inSQL);
		echo $inSQL;
		$this->getContext()->getDBTransaction()->closeTransaction();
	}

	public function update($obj){
		$this->getContext()->getDBTransaction()->startTransaction();
		$inSQL = "UPDATE email
				SET
					email.lang = '".$obj->getLang()."',
					email.type = '".$obj->getType()."',
					email.subject = '".$obj->getSubject()."',
					email.text = '".$obj->getText()."',
					email.html = '".$obj->getHtml()."'
				 WHERE mail_id=".$obj->getMail_id();
		echo $inSQL;
		$this->getMySQLConnection()->mysqlData($inSQL);
		$this->getContext()->getDBTransaction()->closeTransaction();
	}
	
	public function findAllEmail(){
		$this->getContext()->getDBTransaction()->startTransaction();
		$inSQL = "SELECT * FROM email";
		$result = $this->getMySQLConnection()->mysqlRows($inSQL);
		$this->getContext()->getDBTransaction()->closeTransaction();
		return $result;
	}
	
	public function findEmailByOid($oid)
	{
		$this->getContext()->getDBTransaction()->startTransaction();
		$inSQL = "SELECT * FROM email WHERE mail_id=".$oid;
		$rs = $this->getMySQLConnection()->mysqlOneRow($inSQL);
		$this->getContext()->getDBTransaction()->closeTransaction();
		return $rs;
	}
	
	public function findAllEmailByRange($start, $limt)
	{
		$this->getContext()->getDBTransaction()->startTransaction();
		$inSQL = "SELECT * FROM email LIMIT " . $start . " , " . $limt;
		$result = $this->getMySQLConnection()->mysqlRows($inSQL);
		//echo $inSQL;
		$this->getContext()->getDBTransaction()->closeTransaction();
		return $result;
	}
	
	public function countAllEmail(){
		$this->getContext()->getDBTransaction()->startTransaction();
		$inSQL = "SELECT count(mail_id) FROM email";
		//echo $inSQL;
		$result = $this->getMySQLConnection()->mysqlOneField($inSQL);
		$this->getContext()->getDBTransaction()->closeTransaction();
		return $result;
	}
}